package utils;

import cn.hutool.core.util.StrUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: syh
 * @Date: 2023/12/18/12:06
 */
public class DBUtil {
    public static Connection getConnection(String url, String username, String password) {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }


    /**
     * ResultSet转Map
     * @param rs
     * @param metaData
     * @return
     * @throws SQLException
     */
    public static Map<String,String> getFieldMap(ResultSet rs, ResultSetMetaData metaData) throws SQLException {
        Map<String,String> map = new HashMap<>();
        int columnCount = metaData.getColumnCount();
        for (int i = 1; i <= columnCount; i++) {
            String columnName = metaData.getColumnName(i);
            map.put(columnName.toUpperCase(),rs.getString(columnName));
        }

        return map;
    }

    /**
     * ResultSet转Map
     * @param rs
     * @param metaData
     * @return
     * @throws SQLException
     */
    public static List<Map<String,String>> getFieldMapList(ResultSet rs, ResultSetMetaData metaData) throws SQLException {
        List<Map<String,String>> list = new ArrayList<>();
        while (rs.next()) {
            Map<String, String> map = new HashMap<>();
            int columnCount = metaData.getColumnCount();
            for (int i = 1; i <= columnCount; i++) {
                String columnName = metaData.getColumnName(i);
                map.put(columnName.toUpperCase(), rs.getString(columnName));
            }
            list.add(map);
        }

        return list;
    }

    /**
     * 获取数字
     * @param rs
     * @param columnName
     * @return
     * @throws SQLException
     */
    public static Integer getIntValue(ResultSet rs, String columnName) throws SQLException {
        if(rs.next()){
            if(StrUtil.isNotBlank(columnName)){
                return rs.getInt(columnName);
            }else{
                return rs.getInt(1);
            }
        }

        return null;
    }

    /**
     * 获取字符串
     * @param rs
     * @param columnName
     * @return
     * @throws SQLException
     */
    public static String getStringValue(ResultSet rs, String columnName) throws SQLException {
        if(rs.next()){
            if(StrUtil.isNotBlank(columnName)){
                return rs.getString(columnName);
            }else{
                return rs.getString(1);
            }
        }

        return null;
    }

    /**
     * 获取字符串
     * @param rs
     * @param columnName
     * @return
     * @throws SQLException
     */
    public static List<String> getStringValueList(ResultSet rs, String columnName) throws SQLException {
        List<String> strList = new ArrayList<>();
        while (rs.next()){
            if(StrUtil.isNotBlank(columnName)){
                strList.add(rs.getString(columnName));
            }else{
                strList.add(rs.getString(1));
            }
        }

        return strList;
    }
}
